/*
 * Class.cpp
 *
 *  Created on: Aug 5, 2009
 *      Author: ramesh
 */

#include "Class.h"

using namespace SymphonyAST;


string Class::getName() {
	return name;
}
void Class::setName(string name) {
	this->name = name;
}

void Class::import(Class* toImport) {
	importeds.push_back(toImport);
	importIndex++;
}

int Class::getImportedId(string imported) {
	for (int i = 0; i < (int)importeds.size(); i++) {
		if (importeds[i]->getName() == imported) {
			return i;
		}
	}
	return -1;
}

Class* Class::getImported(int i) {
	return importeds[i];
}

void Class::addMethod(string name, Method* method) {
	methods[name] = method;
	methodsList.push_back(name);
}

Method* Class::getMethod(string name) {
	return methods[name];
}

int Class::getMethodId(string name) {
	for (int i = 0; i < (int)methodsList.size(); i++) {
		if (methodsList[i] == name) {
			return i;
		}
	}

	return -1;
}

map<string, Method*> Class::getMethods() {
	return methods;
}

vector<string> Class::getMethodsIds() {
	return methodsList;
}


Variable* Class::getVariable(const string& name) {
	map<string, Variable*>::iterator var = varTable.find(name);

	if (var == varTable.end())
		return NULL;
	else
		return var->second;
}

void Class::addVariable(string name, Variable* member) {
	if (varTable.find(name) != varTable.end()) {
		throw "Duplicate declaration of " + name;
	}

	//not yet reserved
	if (member->getAddress() == -1) {
		member->address = getOffset();
		//sometimes we can add a var of which we don't know the type yet
		offset += member->getSize() == 0 ? 8 : member->getSize();
	}

	varTable[name] = member;
	membersList.push_back(name);
}

vector<Class*> Class::getImporteds() {
	return importeds;
}

vector<string> Class::getMembersIds() {
	return membersList;
}
